package com.xiaoyu.client.ui.activity.main.mine.sign_and_help;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.TimeUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.mine.SignInBean;
import com.xiaoyu.client.network.NetworkConstants;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.fragment.main.MyFragment;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.CheckFormatUtils;
import com.xiaoyu.commonlib.utils.CommonFragmentPagerAdapter;
import com.xiaoyu.commonlib.utils.PreferencesUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.commonlib.utils.ViewPagerUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author jx
 * @date 创建时间 2018/10/17
 * @Description 签到
 */
public class SignActivity extends BaseActivityWithTitle implements ViewPager.OnPageChangeListener {

    @BindView(R.id.integral_shop)
    TextView mIntegralShop;
    @BindView(R.id.integral_record)
    TextView mIntegralRecord;
    @BindView(R.id.activity_sign_viewpager)
    ViewPager mViewPager;
    @BindView(R.id.activity_sign_line)
    View mBarLine; //标题底部划线
    @BindView(R.id.activity_sign_linear)
    LinearLayout mBarView;
    private List<Fragment> fragments = new ArrayList<>();//两个fragment集合
    private CommonFragmentPagerAdapter managementAdapter;

    private ViewPagerUtils mPagerUtils;

    @BindView(R.id.integral_sum)
    TextView mIntegralSum;

    @BindView(R.id.monday)
    TextView monday;
    @BindView(R.id.tuesday)
    TextView tuesday;
    @BindView(R.id.wednesday)
    TextView wednesday;
    @BindView(R.id.thursday)
    TextView thursday;
    @BindView(R.id.friday)
    TextView friday;
    @BindView(R.id.saturday)
    TextView saturday;
    @BindView(R.id.weekday)
    TextView weekday;
    @BindView(R.id.activity_sign_usable_linear)
    LinearLayout mSignUsableLinear;

    @Override
    protected void loadContentView(ViewGroup parent) {
        View mView = LayoutInflater.from(this).inflate(R.layout.activity_sign, parent, true);
        setTitleText("签到");
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mIntegralShop.setOnClickListener(this);
        mIntegralRecord.setOnClickListener(this);

        monday.setOnClickListener(this);
        tuesday.setOnClickListener(this);
        wednesday.setOnClickListener(this);
        thursday.setOnClickListener(this);
        friday.setOnClickListener(this);
        saturday.setOnClickListener(this);
        weekday.setOnClickListener(this);

        fragments.add(new IntegralRecordFragment(0));
        fragments.add(new IntegralRecordFragment(1));

        managementAdapter = new CommonFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(managementAdapter);
        mViewPager.setOnPageChangeListener(this);
        //默认显示第一个
        mViewPager.setCurrentItem(0);
        mPagerUtils = new ViewPagerUtils(this, mBarView, mBarLine, true);
        mPagerUtils.initLine(70, 1);
        mPagerUtils.setTextColor("#333333", "#0c93e9");
        mPagerUtils.selectItem(0);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            if(bundle.getString(MyFragment.MY_INTEGRAL).equals("my_integral")){
                mSignUsableLinear.setVisibility(View.GONE);
            }
        }
        getSignInInfo();

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == mIntegralShop) {
            mViewPager.setCurrentItem(0, true);
            mPagerUtils.selectItem(0);
        }
        if (v == mIntegralRecord) {
            mViewPager.setCurrentItem(1, true);
            mPagerUtils.selectItem(1);
        }
        if (v == monday) {//周一
            if (CheckFormatUtils.getWeeks().equals("星期一") && !CheckFormatUtils.getCurrentDate().equals(PreferencesUtil.getString(PreferencesUtil.IS_SIGN_IN, ""))) {
                monday.setClickable(true);
                monday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
                addSignIn();
            } else {
                monday.setClickable(false);
            }
        }
        if (v == tuesday) {//周二
            if (CheckFormatUtils.getWeeks().equals("星期二") && !CheckFormatUtils.getCurrentDate().equals(PreferencesUtil.getString(PreferencesUtil.IS_SIGN_IN, ""))) {
                tuesday.setClickable(true);
                tuesday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
                addSignIn();
            } else {
                tuesday.setClickable(false);
            }
        }
        if (v == wednesday) {//星期三
            if (CheckFormatUtils.getWeeks().equals("星期三") && !CheckFormatUtils.getCurrentDate().equals(PreferencesUtil.getString(PreferencesUtil.IS_SIGN_IN, ""))) {
                wednesday.setClickable(true);
                wednesday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
                addSignIn();
            } else {
                wednesday.setClickable(false);
            }
        }
        if (v == thursday) {//周四
            if (CheckFormatUtils.getWeeks().equals("星期四") && !CheckFormatUtils.getCurrentDate().equals(PreferencesUtil.getString(PreferencesUtil.IS_SIGN_IN, ""))) {
                thursday.setClickable(true);
                thursday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
                addSignIn();
            } else {
                thursday.setClickable(false);
            }
        }
        if (v == friday) {//周五
            if (CheckFormatUtils.getWeeks().equals("星期五") && !CheckFormatUtils.getCurrentDate().equals(PreferencesUtil.getString(PreferencesUtil.IS_SIGN_IN, ""))) {
                friday.setClickable(true);
                friday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
                addSignIn();
            } else {
                friday.setClickable(false);
            }
        }
        if (v == saturday) {//周六
            if (CheckFormatUtils.getWeeks().equals("星期六") && !CheckFormatUtils.getCurrentDate().equals(PreferencesUtil.getString(PreferencesUtil.IS_SIGN_IN, ""))) {
                saturday.setClickable(true);
                saturday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
                addSignIn();
            } else {
                saturday.setClickable(false);
            }
        }
        if (v == weekday) {//周日
            if (CheckFormatUtils.getWeeks().equals("星期日") && !CheckFormatUtils.getCurrentDate().equals(PreferencesUtil.getString(PreferencesUtil.IS_SIGN_IN, ""))) {
                weekday.setClickable(true);
                weekday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
                addSignIn();
            } else {
                wednesday.setClickable(false);
            }
        }

    }


    /**
     * viewpager的切换监听事件
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        mPagerUtils.scrollLineMove(position, positionOffset);
    }

    @Override
    public void onPageSelected(int position) {
        mPagerUtils.selectItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    //获取签到状态
    private void getSignInInfo() {
        NetworkManager.signIn(new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                succeedResultInfo(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    //解析数据显示
    private void succeedResultInfo(String result) {
        SignInBean dataBean = new Gson().fromJson(result, SignInBean.class);
        mIntegralSum.setText(dataBean.getData().getJifensum().equals("") ? "0" : dataBean.getData().getJifensum());//总积分
        //周一
        if (dataBean.getData().getWeeklist().get(0).getTimecode().equals("true")) {//已签到
            monday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
        } else {
            monday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_weiqiandao, 0, 0);
        }

        //周二
        if (dataBean.getData().getWeeklist().get(1).getTimecode().equals("true")) {//已签到
            tuesday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
        } else {
            tuesday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_weiqiandao, 0, 0);
        }
        //周三
        if (dataBean.getData().getWeeklist().get(2).getTimecode().equals("true")) {//已签到
            wednesday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
        } else {
            wednesday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_weiqiandao, 0, 0);
        }
        //周四
        if (dataBean.getData().getWeeklist().get(3).getTimecode().equals("true")) {//已签到
            thursday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
        } else {
            thursday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_weiqiandao, 0, 0);
        }
        //周五
        if (dataBean.getData().getWeeklist().get(4).getTimecode().equals("true")) {//已签到
            friday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
        } else {
            friday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_weiqiandao, 0, 0);
        }
        //周六
        if (dataBean.getData().getWeeklist().get(5).getTimecode().equals("true")) {//已签到
            saturday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
        } else {
            saturday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_weiqiandao, 0, 0);
        }
        //周日
        if (dataBean.getData().getWeeklist().get(6).getTimecode().equals("true")) {//已签到
            weekday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_yiqianao, 0, 0);
        } else {
            weekday.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.mine_icon_weiqiandao, 0, 0);
        }

    }

    //添加签到
    private void addSignIn() {
        NetworkManager.addSign(new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                ToastUtil.showToast(SignActivity.this, "签到成功");
                getSignInInfo();
                //保存当前时间去判断今天是否签到
                PreferencesUtil.commit(PreferencesUtil.IS_SIGN_IN, CheckFormatUtils.getCurrentDate());
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }
}
